<!--
 * @Description:
 * @Author: Richeneen
 * @Date: 2022-10-12 15:22:48
 * @LastEditTime: 2022-10-23 16:48:00
 * @LastEditors: Richeneen
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <!-- 增加按钮 -->
    <el-button type="primary" @click="toAdd">增加</el-button>
    <!-- 资讯分类表单 -->
    <!-- 第二行 表格 -->
    <div class="table">
      <el-table :data="categories" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" />
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="no" label="排序号" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tag type="success" @click="toEdit(scope.row)">编辑</el-tag>
            <el-tag type="danger" @click="toDelete(scope.row.id)">删除</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 第二行 表格 结束 -->
    <!-- 嵌套表单的dialog -->
    <div class="dialog">
      <el-dialog :title="title" :visible.sync="visible">
        <el-form :model="form" :label-position="labelPosition">
          <el-form-item label="栏目名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              autocomplete="off"
              label-width="80px"
            />
          </el-form-item>
          <el-form-item label="序号" :label-width="formLabelWidth">
            <el-input
              v-model="form.no"
              autocomplete="off"
              label-width="80px"
            />
          </el-form-item>
          <el-form-item label="栏目介绍">
            <el-input
              v-model="form.description"
              type="textarea"
              :label-width="formLabelWidth"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 嵌套表单的dialog 结束 -->
  </div>
</template>

<script>
import { updateOrAddCategory, deleteCategory } from '@/api/infoCategory'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      // dialog
      visible: false,
      // dialog标题
      title: '',
      // dialog 嵌套表单
      form: {},
      formLabelWidth: '120px',
      labelPosition: 'left'
    }
  },
  computed: {
    // 获取资讯列表
    ...mapGetters(['categories'])
  },
  created() {
    this.$store.dispatch('category/queryCategory')
  },
  mounted() {},
  methods: {
    // 编辑按钮事件
    toEdit(row) {
      this.visible = true
      this.form = { ...row }
      this.title = '编辑栏目信息'
      // console.log(this.form);
    },
    // 删除按钮事件
    toDelete(id) {
      this.$confirm('此操作将删除该栏目, 确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteCategory({ id: id })
          this.$message({
            type: 'success',
            message: res.message
          })
          this.$store.dispatch('category/queryCategory')
        })

        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增按钮事件
    toAdd() {
      this.visible = true
      this.title = '新增栏目信息'
      // 将模态框清空
      this.form = {}
    },
    // 模态框确定事件
    toSubmit() {
      this.$confirm('确认保存？').then(async() => {
        const res = await updateOrAddCategory(this.form)
        this.$message({
          type: 'success',
          message: res.message
        })
        this.visible = false
        this.$store.dispatch('category/queryCategory')
      })
    }
  }
}
</script>
<style scoped>
</style>
